package com.aijia.custom.product.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aijia.custom.product.R;
import com.aijia.custom.product.data.SheetData;
import com.aijia.custom.product.data.WSheetData;
import com.aijia.custom.product.ui.OrderItemActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yejianjun on 2019/1/16.
 */

public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_HEAD = 2;
    private static final int TYPE_AD = 3;
    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    //没有加载更多 隐藏
    public static final int NO_LOAD_MORE = 2;
    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;
    LayoutInflater mInflater;
    private List<WSheetData> mList = new ArrayList<>();
    private Context mContext;
    private OnItemClickListener onItemClickListener;

    public OrderAdapter(Context ctx) {
        mContext = ctx;
        mInflater = LayoutInflater.from(ctx);
    }

    public void setData(List<WSheetData> list) {
        if (null == mList) {
            mList = new ArrayList<>();
        }
        mList = list;
    }

    /**
     * 向后追加数据
     *
     * @param list
     */
    public void insertData(List<WSheetData> list) {
        if (null == mList) {
            mList = new ArrayList<>();
        }

        mList.addAll(mList.size(), list);
    }

    public List<WSheetData> getList() {
        if (null == mList)
            mList = new ArrayList<>();
        return mList;
    }

    public void clear() {
        if (null != mList) {
            mList.clear();
        }
    }

    /**
     * 单个数据更新需优化
     *
     * @param id
     * @param artId
     * @param status
     * @param finishDate
     */
    public void updateStatus(int id, int artId, int status, String finishDate) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getId() == id) {
                for (int j = 0; j < mList.get(i).getItems().size(); j++) {
                    if (mList.get(i).getItems().get(j).getId() == artId) {
                        mList.get(i).getItems().get(j).setStatus(status);
                        mList.get(i).getItems().get(j).setFinishDate(finishDate);
                        notifyItemChanged(i);
                        break;
                    }
                }
                break;
            }
        }
    }

    /**
     * 更新外层网店定金状态 ：
     *
     * @param id
     * @param status 1：生产中 2： 部分完成 3： 全部完成 4：自己联系
     */
    public void updateListStatus(int id, int status) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getId() == id) {
                mList.get(i).setStatus(status);
                notifyItemChanged(i);
                break;
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            View itemView = mInflater.inflate(R.layout.load_more_footview_layout, parent, false);
            return new FooterViewHolder(itemView);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.order_list_item, parent, false);
            return new ItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            WSheetData resp = mList.get(position);

            itemViewHolder.shopNameTV.setText("店铺名：" + (resp.getShopName() == null ? "" : resp.getShopName()));
            itemViewHolder.customerTV.setText("产品：" + (resp.getProductName() == null ? "" : resp.getProductName()));
            itemViewHolder.originatorTV.setText("发起人：" + (resp.getStaffName() == null ? "" : resp.getStaffName()));
            itemViewHolder.createDateTV.setText("日期：" + (resp.getUnderDate() == null ? "" : resp.getUnderDate()));
            itemViewHolder.nameTV.setText("名称：" + (resp.getName() == null ? "" : resp.getName()));
            itemViewHolder.wechatTV.setText("微信：" + (resp.getWx() == null ? "" : resp.getWx()));
            itemViewHolder.contentTV.setText("备注：" + (resp.getContent() == null ? "" : resp.getContent()));

            if (resp.getStatus() == 1) {
                itemViewHolder.statusIV.setImageResource(R.drawable.icon_red);
            } else if (resp.getStatus() == 2) {
                itemViewHolder.statusIV.setImageResource(R.drawable.icon_yellow);
                itemViewHolder.moneyDescTV.setText("定金：未收");
            } else if (resp.getStatus() == 3) {
                itemViewHolder.statusIV.setImageResource(R.drawable.icon_green);
                itemViewHolder.moneyDescTV.setText("定金：已收");
            } else if (resp.getStatus() == 4) {
                itemViewHolder.statusIV.setImageResource(R.drawable.half_color);
                itemViewHolder.moneyDescTV.setText("发起人跟踪");
            }

            if (resp.getContacted() == 0) {
                itemViewHolder.contactedTV.setText("未联系");
            } else {
                itemViewHolder.contactedTV.setText("已联系");
            }

            itemViewHolder.layout.setOnClickListener(new MyClickListener(resp));
//            itemViewHolder.layout.setTag(R.id.order_item_layout, position);

        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            switch (mLoadMoreStatus) {
                case PULLUP_LOAD_MORE:
                    footerViewHolder.mPbLoad.setVisibility(View.GONE);
                    footerViewHolder.mTvLoadText.setText("上拉加载更多...");
                    footerViewHolder.mLoadLayout.setVisibility(View.VISIBLE);
                    break;
                case LOADING_MORE:
                    footerViewHolder.mPbLoad.setVisibility(View.VISIBLE);
                    footerViewHolder.mTvLoadText.setText("正在加载...");
                    footerViewHolder.mLoadLayout.setVisibility(View.VISIBLE);
                    break;
                case NO_LOAD_MORE:
                    //隐藏加载更多
                    if (mList.size() >= 10) {
                        footerViewHolder.mTvLoadText.setText("没有更多数据");
                        footerViewHolder.mPbLoad.setVisibility(View.GONE);
                        footerViewHolder.mLoadLayout.setVisibility(View.VISIBLE);
                    } else {
                        footerViewHolder.mLoadLayout.setVisibility(View.GONE);
                    }
                    break;
                default:
                    break;
            }

        }
    }

    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            //最后一个item设置为footerView
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    /**
     * 更新加载更多状态
     *
     * @param status
     */

    public void changeMoreStatus(int status) {
        mLoadMoreStatus = status;
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (null != onItemClickListener) {
            onItemClickListener.itemClick(v);
        }
    }

    /**
     * 设置回调监听
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void itemClick(View view);
    }


    class FooterViewHolder extends RecyclerView.ViewHolder {
        ProgressBar mPbLoad;
        TextView mTvLoadText;
        LinearLayout mLoadLayout;

        public FooterViewHolder(View itemView) {
            super(itemView);
            mPbLoad = (ProgressBar) itemView.findViewById(R.id.pbLoad);
            mTvLoadText = (TextView) itemView.findViewById(R.id.tvLoadText);
            mLoadLayout = (LinearLayout) itemView.findViewById(R.id.loadLayout);
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        // 店铺名
        private TextView shopNameTV;
        // 产品
        private TextView customerTV;
        // 发起人
        private TextView originatorTV;
        // 时间
        private TextView createDateTV;
        // 客户名称
        private TextView nameTV;
        // 微信
        private TextView wechatTV;
        // 备注
        private TextView contentTV;
        // 状态
        private ImageView statusIV;
        private TextView moneyDescTV;
        // 是否联系
        private TextView contactedTV;
        private LinearLayout layout;


        public ItemViewHolder(View itemView) {
            super(itemView);
            shopNameTV = itemView.findViewById(R.id.order_item_shopName);
            customerTV = itemView.findViewById(R.id.order_item_customer);
            originatorTV = itemView.findViewById(R.id.order_item_originator);
            createDateTV = itemView.findViewById(R.id.order_item_createtime);
            nameTV = itemView.findViewById(R.id.order_item_name);
            wechatTV = itemView.findViewById(R.id.order_item_wechat);
            contentTV = itemView.findViewById(R.id.order_item_content);
            statusIV = itemView.findViewById(R.id.order_item_status);
            moneyDescTV = itemView.findViewById(R.id.order_item_money_desc);
            contactedTV = itemView.findViewById(R.id.contact_status_tv);
            layout = itemView.findViewById(R.id.order_item_layout);
        }
    }

    class MyClickListener implements View.OnClickListener {
        private WSheetData data;

        public MyClickListener(WSheetData data) {
            this.data = data;
        }

        @Override
        public void onClick(View v) {
            SheetData sheetData = new SheetData();
            sheetData.setId(data.getId());
            sheetData.setProductName(data.getProductName());
            sheetData.setName(data.getName());
            sheetData.setWx(data.getWx());
            sheetData.setMobile(data.getMobile());
            sheetData.setAddress(data.getAddress());
            sheetData.setContent(data.getContent());
            sheetData.setUnderDate(data.getUnderDate());
            sheetData.setStaffName(data.getStaffName());
            sheetData.setUnderDate(data.getUnderDate());
            sheetData.setWwId(data.getWwId());
            sheetData.setShopName(data.getShopName());
            sheetData.setStatus(data.getStatus());
            sheetData.setContacted(data.getContacted());
            sheetData.setStaffId(data.getStaffId());
            Intent intent = new Intent();
            intent.setClass(mContext, OrderItemActivity.class);
            intent.putExtra("sheetData", sheetData);
            intent.putParcelableArrayListExtra("items", data.getItems());
            mContext.startActivity(intent);
        }
    }


}
